home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10553 < prev    next >
Encoding:
Text File  |  1996-08-05  |  846 b   |  48 lines

  1. Path: news.uh.edu!sukku
  2. From: sukku@menudo.uh.edu (sukumar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Dynamic alloc of 2D array??
  5. Date: 8 Mar 1996 15:22:32 GMT
  6. Organization: University of Houston
  7. Message-ID: <4hpjbo$fsb@masala.cc.uh.edu>
  8. NNTP-Posting-Host: menudo.uh.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Hi,
  12.     I am trying to allocate a 2D array. This is how I am doing it:
  13.  
  14. char** cities;
  15.  
  16.  
  17. // Alloc x number of char*s
  18. cities = new (char* [x]);
  19.  
  20.  
  21. // Alloc bytes to each city 
  22. for(i=0; i<x; i++)
  23.     cities[i] = new char[y];
  24.  
  25.  
  26. ....
  27.  
  28. I copy a string to each city in some cases
  29.  
  30. and some times all the cities are not assigned any strings
  31.  
  32.  
  33. ....
  34.  
  35.  
  36. for(i=0; i<x; i++)
  37.     delete [] cities[i];
  38. delete [] cities;
  39.  
  40.  
  41.  
  42. The program crashes when I don't assign anything to the cities.
  43. What is the right way to alloc an array of pointers and delete them??
  44.  
  45.  
  46. TIA,
  47. SK
  48.